
package gui.panels;

import gui.MainFrame;
import gui.listeners.GuiActionEvent;
import gui.listeners.GuiActionEventData;
import gui.listeners.GuiActionEventType;
import gui.model.DrinkModel;
import gui.panels.components.MmiButton;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

/**
 *
 * @author JtR
 */
public class SelectCoffeeAdditionsPanel extends GuiPanel {
    private JPanel info;
    private MmiButton caramelButton, hazelnutButton, milkButton, doneButton;
    private JCheckBox hazelnutCheckbox, caramelCheckbox, milkCheckbox;
    private boolean isCappuchino;
    
    private boolean selectedHazelnut = false;
    private boolean selectedCaramel = false;
    private boolean selectedMilk = false;
    
    public SelectCoffeeAdditionsPanel(MainFrame parent, boolean isCappucchino) {
        super(parent, BackgroundType.COFFEE);
        this.isCappuchino = isCappucchino;
        initComponents(isCappucchino);
    }
    
    protected void initComponents(boolean isCappuccino) {
        caramelButton = new MmiButton("res/kaffeepause/Karamell.png", 88);
        caramelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectedCaramel = !selectedCaramel;
                updateAdditionCheckboxes();
            }
        });
        constraints.insets = new Insets(193, 269, 0, 0);
        add(caramelButton, constraints);
        
        hazelnutButton = new MmiButton("res/kaffeepause/haselnuss.png", 88);
        hazelnutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectedHazelnut = !selectedHazelnut;
                updateAdditionCheckboxes();
            }
        });
        constraints.insets = new Insets(230, 375, 0, 0);
        add(hazelnutButton, constraints);
        
        if(!isCappuccino) {
            milkButton = new MmiButton("res/kaffeepause/milch.png", 472);
            milkButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    selectedMilk = !selectedMilk;
                    updateAdditionCheckboxes();
                }
            });
            constraints.insets = new Insets(94, 396, 0, 0);
            add(milkButton, constraints);
        }
        
        info = new JPanel(new GridBagLayout());
        info.setBackground(Color.WHITE);
        info.setBorder(new LineBorder(Color.BLACK));
        constraints.insets = new Insets(0, 0, 0, 0);
        constraints.anchor = GridBagConstraints.SOUTHEAST;
        add(info, constraints);
        
        JPanel subInfo = new JPanel(new GridBagLayout());
        subInfo.setBackground(Color.WHITE);
        
        GridBagConstraints subInfoConstraints = new GridBagConstraints();
        subInfoConstraints.gridx = 0;
        
        subInfoConstraints.gridy = GridBagConstraints.RELATIVE;
        subInfoConstraints.anchor = GridBagConstraints.WEST;
        subInfoConstraints.insets = new Insets(8, 8, 0, 0);
        JLabel infoLabel = new JLabel("Sie wählten:");
        infoLabel.setFont(infoLabel.getFont().deriveFont(15f));
        subInfo.add(infoLabel, subInfoConstraints);
        
        subInfo.setBorder(new LineBorder(Color.BLACK));
        
        caramelCheckbox = new JCheckBox("Karamel");
        caramelCheckbox.setEnabled(false);
        caramelCheckbox.setBackground(Color.WHITE);
        caramelCheckbox.setFont(caramelCheckbox.getFont().deriveFont(15f));
        subInfoConstraints.insets = new Insets(0, 20, 0, 8);
        subInfo.add(caramelCheckbox, subInfoConstraints);
        
        hazelnutCheckbox = new JCheckBox("Haselnuss");
        hazelnutCheckbox.setEnabled(false);
        hazelnutCheckbox.setBackground(Color.WHITE);
        hazelnutCheckbox.setFont(hazelnutCheckbox.getFont().deriveFont(15f));
        if(isCappuccino)
            subInfoConstraints.insets = new Insets(0, 20, 8, 8);
        subInfo.add(hazelnutCheckbox, subInfoConstraints);
        
        if(!isCappuccino) {
            milkCheckbox = new JCheckBox("Milch");
            milkCheckbox.setEnabled(false);
            milkCheckbox.setBackground(Color.WHITE);
            milkCheckbox.setFont(milkCheckbox.getFont().deriveFont(15f));
            subInfoConstraints.insets = new Insets(0, 20, 8, 8);
            subInfo.add(milkCheckbox, subInfoConstraints);
        }
        
        
        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = GridBagConstraints.RELATIVE;
        constraints.insets = new Insets(15, 15, 0, 15);
        info.add(subInfo, constraints);
        
        doneButton = new MmiButton("res/kaffeepause/fertigKaffee.png", 70);
        doneButton.setFont(doneButton.getFont().deriveFont(15f));
        doneButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.COFFEE_DONE, new GuiActionEventData(selectedCaramel, selectedHazelnut, selectedMilk)));
            }
        });
        constraints.insets = new Insets(15, 0, 15, 15);
        info.add(doneButton, constraints);
    }
    
    private void updateAdditionCheckboxes() {
        caramelCheckbox.setSelected(selectedCaramel);
        hazelnutCheckbox.setSelected(selectedHazelnut);
        if(!isCappuchino)
            milkCheckbox.setSelected(selectedMilk);
    }

    @Override
    public String getDescription() {
        return "Bitte wählen Sie Ihre Zusätze";
    }

    @Override
    public void updateFields() {
        selectedCaramel = DrinkModel.getInstance().isCaramel();
        selectedHazelnut = DrinkModel.getInstance().isHazelnut();
        if(!isCappuchino)
            selectedMilk = DrinkModel.getInstance().isMilk();
        updateAdditionCheckboxes();
    }

    @Override
    public boolean getPreviousButtonEnabled() {
        return true;
    }

    @Override
    public boolean getNextButtonEnabled() {
        return false;
    }

    @Override
    public String getName() {
        if(!isCappuchino)
            return "COFFEE_ADDITIONS_NAME";
        else
            return "CAPPUCHINO_ADDITIONS_NAME";
    }

    @Override
    public GuiPanel getNextPanel() {
        return parent.finishPanel;
    }

    @Override
    public GuiPanel getPreviousPanel() {
        if(DrinkModel.getInstance().getCoffee() == DrinkModel.Coffee.BLACK_COFFEE || 
            DrinkModel.getInstance().getCoffee() == DrinkModel.Coffee.CAPPUCHINO) {
            return parent.selectBeansPanel;
        } else {
            return parent.selectEspressoSizePanel;
        }
    }
}